package client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/***
 * @author: SJun
 * @Date: 2020/8/13 9:39
 * @description:
 *
 */
public class ClientMain {

    public static Bootstrap clientBootstrap;

    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {


                EventLoopGroup group = new NioEventLoopGroup();
                try {
                    clientBootstrap = new Bootstrap();

                    clientBootstrap.group(group);
                    clientBootstrap.channel(NioSocketChannel.class);
                    clientBootstrap.remoteAddress(new InetSocketAddress("127.0.0.1", 2000));
                    clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline()
//                            .addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 1, -1, 1))
                                    .addLast(new ClientHandle());
                        }
                    });
                    ChannelFuture channelFuture = clientBootstrap.connect();
                    channelFuture.channel().closeFuture();
                } finally {
                    //group.shutdownGracefully();
                }
            }
        }).start();
    }


    public static void main(String[] args) {
        new ClientMain().start();

       /* Device.deviceChannel.forEach((key,value)->{
            int[] data = {0x30, 0x00, 0x10};
            key.writeAndFlush(ByteBufUtil.hexToByteBuf(data));
        });*/
    }
}
